let tableIns;
let tree;
layui.use(['table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    tree = layui.tree;
    let laydate = layui.laydate;
    let form = layui.form;
    let height = document.documentElement.clientHeight - 60;

    tableIns = table.render({
        elem: '#balanceTable'
        , url: '/seller/qryBalance/page'
        , method: 'POST'
        , where: {
        	sellerCompanyName: ''
        }
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'rows' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'records' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            // console.log("data=="+data);
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": data.records, //解析数据长度
                "rows": data.rows //解析数据列表
            };
        }
        , toolbar: true
        , title: '商户余额'
        , cols: [[
            {field: 'sellerCompanyName', title: '公司名字'}
            , {field: 'accountTotal', title: '余额', templet:function (d) {
                    return '￥' + formatMoney(d.accountTotal) + '元';
                }}
            ,{fixed: 'right', title:'操作', toolbar: '#barBtn'}
        ]]
        , defaultToolbar: ['', '', '']
        , page: true
        , height: height
        , cellMinWidth: 80
    });

    table.on('tool(display)',function (obj) {
        var _data = obj.data;
        console.log(_data);
        console.log("id====="+_data.sellerId);
        if(obj.event === 'detailed_a'){
            layer.open({
                type: 2,
                title: '商户余额明细',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['800px', '450px'],
                shadeClose: true, //开启遮罩关闭
                content: '/seller/balance/dtls?'+_data.sellerId,
                success: function(layero, index){

                }
            });
        }
        
        // 加款
        else if (obj.event === 'addmoney') {
        	layer.open({
                type: 2,
                title: '商户加款',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['600px', '400px'],
                shadeClose: true, //开启遮罩关闭
                content: '/seller/addmoney/'+_data.sellerId,
                success: function(layero, index){
                	
                }
            });
        }	
        
        // 扣款
        else if (obj.event === 'delmoney') {
        	layer.open({
                type: 2,
                title: '商户扣款',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['600px', '400px'],
                shadeClose: true, //开启遮罩关闭
                content: '/seller/delMoney/'+_data.sellerId,
                success: function(layero, index){
                	
                }
            });
        }
    });
    
    
    form.on('submit(menu-search)', function (data) {
      	 // 搜索条件
          var sellerCompanyName=data.field.sellerCompanyName;
        // var tel=$("#tel").val();
          table.reload('balanceTable', {
              url:'/seller/qryBalance/page'
              , where: {
            	  sellerCompanyName : sellerCompanyName
              }
              , page: {
                  curr: 1
              }
          });
          return false;
          
     });


       
       




});


function formatMoney(str) {
    if (!str) str = '0.00';
    let ret = Math.round(parseFloat(str) * 100) / 100;
    let decimal = ret.toString().split('.');
    if (decimal.length === 1) {
        return ret.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            + '.00';
    };
    if (decimal.length > 1) {
        decimal[0] = decimal[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        if (decimal[1].length < 2) {
            decimal[1] += '0';
            return decimal.join(".");
        }
        return decimal.join(".");
    };
    return ret;
}




/**
 * 提交保存
 */
function accountFormSave() {
    let accountForm = $("#accountForm").serializeObject();
    var reg = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/

    if(!reg.test(accountForm.operationMoney) && null != accountForm.operationMoney){
        layer.msg('金额输入不正确',{icon: 5});
        return false;
    }
    
    if(accountForm.tradeComment == null || accountForm.tradeComment == ''){
    	 layer.msg('备注为必填项',{icon: 5});
         return false;
    }
    
    
    save();
}

function save(){
	let accountForm = $("#accountForm").serializeObject();
    $.post(ctx + "/seller/save", accountForm, function (data) {
    	console.log(data);
    	if(data.flag){
	        layer.msg("商户调额成功", {icon: 1, time: 2000}, function(){
	        	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	        	parent.layer.close(index); //再执行关闭
	        	window.parent.$("#queryByAgentNameBtn").click();
	        });
    	} else {
    		layer.msg(data.data, {icon: 5}); 
    	}
    });
}



